package com.lanou.myapp.funny.textfunny;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.myapp.R;
import com.lanou.myapp.progressdialog.Dialog;

import java.util.ArrayList;

/**
 * Created by dllo on 16/1/12.
 */
public class TextFragment extends Fragment{
    private String url = "http://s.budejie.com/topic/list/jingxuan/29/budejie-android-6.2.7/0-20.json?market=wandoujia&ver=6.2.7&visiting=&os=5.1&appname=baisibudejie&client=android&udid=000000000000000&mac=08%3A00%3A27%3A3f%3A7a%3A01";
    private Context context;
    private RecyclerView recyclerView;
    private RecyclerAdapter adapter;
    private RequestQueue requestQueue;
    private ArrayList<ListData> data;
    private Dialog dialog;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        dialog = new Dialog(context,"");
        dialog.setTitle("正在加载，请稍后...");
        dialog.show();
        recyclerView = (RecyclerView) getActivity().findViewById(R.id.recyclerView_funny);

        requestQueue = Volley.newRequestQueue(getActivity());

        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                Gson gson = new Gson();
                FunnyBeans beans = gson.fromJson(response, FunnyBeans.class);
                data = new ArrayList<>();
                for (int i = 0; i < beans.getList().size(); i++) {
                    data.add(beans.getList().get(i));
                }

                adapter = new RecyclerAdapter(getActivity(),data);
                recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                recyclerView.setAdapter(adapter);
                dialog.dismiss();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        requestQueue.add(request);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_text_funny,null);
        return view;
    }
}
